import 'package:chewie/chewie.dart';
import 'package:fijkplayer/fijkplayer.dart';
import 'package:flutter_ncsjqz_getx/api/api.dart';
import 'package:flutter_ncsjqz_getx/http/dio_service.dart';
import 'package:flutter_ncsjqz_getx/model/commodity_details_entity.dart';
import 'package:flutter_ncsjqz_getx/page/farm/order/order_view.dart';
import 'package:flutter_ncsjqz_getx/util/toast_util.dart';
import 'package:get/get.dart';
import 'package:package_get/base_logic.dart';
import 'package:video_player/video_player.dart';

class FarmDetailsLogic extends BaseLogic {
  FijkPlayer? player;
  ChewieController? chewieController;
  VideoPlayerController? videoPlayerController1;
  int count = 1; // 数量
  int onePrice = 0; // 规格下的商品单价
  int countPrice = 0; // 总价
  CommodityDetailsData? commodityDetailData = CommodityDetailsData();
  String selectSku = "";
  CommodityDetailsDataSkuList commodityDetailsDataSkuList = CommodityDetailsDataSkuList();

  getPlay(url) async{
    // player!.setDataSource(url ?? Api.testVideo1,showCover: true);
    videoPlayerController1 = VideoPlayerController.network(url ?? Api.testVideo1);
    await videoPlayerController1!.initialize();
    chewieController = ChewieController(
      videoPlayerController: videoPlayerController1!,
      autoPlay: false,
      looping: true,
      showOptions: false,
    );
    update();
  }

  /// 获取商品详情
  getData(commodityId,shopId) async{
    Map<String,dynamic> map = {
      "commodityId": commodityId,
      "shopId": shopId,
    };
    CommodityDetailsEntity commodityDetailEntity = await DioService<CommodityDetailsEntity>().commodityDetail(map);
    if(commodityDetailEntity.code == 0) {
      commodityDetailData = commodityDetailEntity.data;
      setOnePrice(commodityDetailData!.price!);
      setCountPrice();
      if(commodityDetailData!.commodityVideo != null && commodityDetailData!.commodityVideo != ""){
        getPlay(commodityDetailData!.commodityVideo);
      }
    }
    update();
  }

  /// 计算总价
  setCountPrice(){
    countPrice = onePrice * count;
    update();
  }

  /// 单价赋值
  setOnePrice(int value){
    onePrice = value;
    update();
  }

  /// sku校验 返回当前sku的数量  -1没有找到
  checkShopSku(){
    String skuStr='';
    // 组装我点击的sku
    commodityDetailData!.props!.forEach((element) {
      element.propValues!.forEach((element2) {
        if(element2.select==null || element2.select==false){
          commodityDetailsDataSkuList = CommodityDetailsDataSkuList();
        }else{
          int indexWhere =element.propValues!.indexWhere((element2) => element2.select == null ? false : element2.select! );
          CommodityDetailsDataPropsPropValues shopDataContentPropsPropsPropValue = element.propValues![indexWhere];
          skuStr+=element.propName! +':'+shopDataContentPropsPropsPropValue.propValue!+';';
        }
      });
    });
    int indexWhere = -1;
    int index = skuStr.lastIndexOf(';');
    print('点击的skuStr 未去除尾部分号:$skuStr');
    if(index == skuStr.length-1){//有最后一个分号 需要去掉
      skuStr=skuStr.substring(0,index);
    }
    print('点击的skuStr 去除尾部分号:$skuStr');
    for(int i=0;i<commodityDetailData!.skuList!.length;i++){
      int index=commodityDetailData!.skuList![i].properties!.lastIndexOf(';');//是否有最后的分号
      String? properties=commodityDetailData!.skuList![i].properties;
      if(index==commodityDetailData!.skuList![i].properties!.length-1){//有
        properties=properties!.substring(0,index);
      }
      print("skuList.properties: $properties,点击的skuStr:$skuStr");
      if(properties==skuStr){ // 找到了
        indexWhere=i;
        break;
      }
    }
    // indexWhere == -1 未在skuList集合中找到对应的
    if(indexWhere==-1){
      commodityDetailsDataSkuList=CommodityDetailsDataSkuList();
    }else{
      commodityDetailsDataSkuList=commodityDetailData!.skuList![indexWhere];
      setOnePrice(commodityDetailsDataSkuList.oriPrice!);
      setCountPrice();
    }
    if(commodityDetailsDataSkuList.actualStocks==null){
      toast('当前点击组合未在sku中找到，商品设置规格有问题，请联系管理员');
      print("当前组合 未在 skuList中找到");
    }
    print('skuStr：$skuStr   库存:${commodityDetailsDataSkuList.actualStocks}   价格：${commodityDetailsDataSkuList.oriPrice}');
  }

  /// 默认选择
  setDefSku(){
    String str='';
    commodityDetailData!.props!.forEach((element) {
      CommodityDetailsDataPropsPropValues shopDataContentPropsPropsPropValue =element.propValues![0];
      str+=element.propName! +':'+shopDataContentPropsPropsPropValue.propValue!+';';
      element.propValues!.forEach((element1) {
        element1.select = false;
      });
    });
    ///  默认选择第一个
    commodityDetailData!.props!.forEach((element) {
      element.propValues![0].select=true;
    });
    int indexWhere=-1;
    str=str.substring(0,str.length-1);
    for(int i=0;i<commodityDetailData!.skuList!.length;i++){
      int index=commodityDetailData!.skuList![i].properties!.lastIndexOf(';');
      String? properties=commodityDetailData!.skuList![i].properties;
      if(index==commodityDetailData!.skuList![i].properties!.length-1){
        print('当前SKU有结尾分号:${properties}');
        properties=properties!.substring(0,properties.length-1);
      }
      if(properties == str){
        indexWhere=i;
        break;
      }
    }
    if(indexWhere==-1){
      commodityDetailsDataSkuList=CommodityDetailsDataSkuList();
    }else{
      commodityDetailsDataSkuList = commodityDetailData!.skuList![indexWhere];
    }
    print('skuStr：$str   库存:${commodityDetailsDataSkuList.actualStocks}   价格：${commodityDetailsDataSkuList.oriPrice}');
  }
  /// 提交订单
  submitShop(){
    if(commodityDetailsDataSkuList.skuId == null) return toast("未选择土地");
    if(commodityDetailsDataSkuList.actualStocks! - count < 0){
      toast('仓库没有那么多货哦～～');
    }else{
      commodityDetailData!.amount =count;
      Get.to(()=>OrderPage(
        commodityDetailData: commodityDetailData,
        commodityDetailsDataSkuList: commodityDetailsDataSkuList,
      ), preventDuplicates: false, );
      // Get.back();
    }
  }
}
